package com.bwie.controller;

import com.bwie.domain.TbProd;
import com.bwie.service.IUserService;
import com.bwie.service.TbProdService;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2024/11/13 16:42
 */

@RestController
@RequestMapping(value = "/prod/prod")
public class TbProdController {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    IUserService iUserService;

    @Autowired
    TbProdService tbProdService;

    @PostMapping(value = "/getUserInfoById/{userId}")
    public R getUserInfoById(@PathVariable("userId") Integer userId) {
        R r = restTemplate.postForObject("http://127.0.0.1:9001/user/user/getUserInfo/" + userId, null, R.class);
        return r;
    }

    @PostMapping(value = "/getUserInfo/{userId}")
    public R getUserInfo(@PathVariable("userId") Integer userId) {
        return iUserService.getUserById(userId);
    }

    /**
     * 获取商品列表
     * @return
     */
    @PostMapping(value = "/getProdList")
    public R getProdList(@RequestBody PageInfoVo pageInfoVo) {
        return tbProdService.getProdList(pageInfoVo);
    }

    /**
     * 获取商品状态列表
     * @return
     */
    @PostMapping(value = "/getProdStatusList")
    public R getProdStatusList() {
        return tbProdService.getProdStatusList();
    }

    @PostMapping(value = "/uploadFile")
    public R uploadFile(@RequestPart("file") MultipartFile file) {
        return tbProdService.uploadFile(file);
    }

    /**
     * 添加商品
     * @return
     */
    @PostMapping(value = "/addProd")
    public R addProd(@RequestBody TbProd tbProd) {
        return tbProdService.addProd(tbProd);
    }

    /**
     * 修改商品
     * @param tbProd
     * @return
     */
    @PostMapping(value = "/updateProd")
    public R updateProd(@RequestBody TbProd tbProd) {
        return tbProdService.updateProd(tbProd);
    }

    @PostMapping(value = "/deleteProd/{prodId}")
    public R deleteProd(@PathVariable("prodId") Integer prodId) {
        return tbProdService.deleteProd(prodId);
    }

    /**
     * 发送kafka消息
     * @param message
     * @return
     */
    @PostMapping(value = "/sendKafkaMessage/{message}")
    public R sendKafkaMessage(@PathVariable("message") String message) {
        System.out.println("发送消息：" + message);
        return tbProdService.sendKafkaMessage(message);
    }

    /**
     * 商品上架
     * @return
     */
    @PostMapping(value = "/uploadProd/{prodId}")
    public R uploadProd(@PathVariable("prodId") Integer prodId) {
        return tbProdService.uploadProd(prodId);
    }

    /**
     * 从ES中分页获取产品信息
     * @param pageInfoVo
     * @return
     */
    @PostMapping(value = "/getProdListFromEs")
    public R getProdListFromEs(@RequestBody PageInfoVo pageInfoVo) {
        return tbProdService.getProdListFromEs(pageInfoVo);
    }
}
